Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### The Alias, Private, And Public Keywords These three keywords are optional components of a Declare statement. The Alias keyword lets you define an alternate name, or alias, for the DLL procedure. This allows you to use a different name to call the procedure—a name other than the one it is assigned in the DLL. For example, the statement
    Declare Function APIProc1 Lib “MyDLL” Alias “APIProc2” () as Long
declares that the DLL named MyDLL contains a procedure named APIProc2, which we will call APIProc1 in the program. Why are aliases necessary? The most common reason is that some DLL procedures have the same name as a Visual Basic reserved word. In this situation, trying to use the DLL procedure under its “real” name can cause confusion and errors. By assigning an alias, you can use both the DLL procedure and the reserved word without conflict. Other uses of Alias are internal to the API and are not related to Visual Basic itself. Declare statements that you copy from the API Text Viewer already contain the required aliases. The only time you’ll need to add one yourself is when a DLL function name conflicts with a Basic function or procedure that you defined in your program. Using the Public and Private keywords allows you to control where in your program an API declaration is effective. This determines which of the program’s modules can use the declared procedure. If a procedure is declared Public, you can use it in all of the program’s modules. Public is the default, so including the keyword in the Declare statement is optional. A procedure declared Private can be used only by code in the module that contains the Declare statement. The Public or Private keyword goes at the beginning of the Declare statement:
    Public Declare Sub APIProc1
    Private Declare Function APIProc2
Within a form module, only Private procedure declarations are allowed, meaning that you can place Public declarations only in code modules. The primary use for making a procedure declaration Private occurs when you are writing a form module that you intend to use in other programs as a software component. A software component needs to be completely portable. This means that any API procedures it uses must be declared within the module, requiring the Private keyword. This ensures that the module is completely self-contained. #### Declaring API Functions As Procedures Most of the procedures in the API are functions—in other words, they return a value to the calling program. While your program will often need the return value, this will not always be the case. When an API function’s return value is not needed, you can declare and use the API procedure as one that does not return a value, simplifying its use in the program. For example, the SetTextAlign API procedure specifies the alignment of text that is printed on the screen. It is a function with the following declaration:
    Declare Function SetTextAlign Lib “gdi32” (ByVal hDC As Long, _
        ByVal wFlags As Long) As Long
A Visual Basic program would call it this way:
    RetVal = SetTextAlign(...)
The value this API function returns is the previous text alignment setting. This value is not much use to most programs. To use SetTextAlign as a procedure, you would declare it as
    Declare Sub SetTextAlign Lib “GDI” (ByVal hDC As Long, ByVal wFlags _
       As Long)
and call it using the Call statement:
    Call SetTextAlign(...)
You cannot define an API procedure as both a function and a procedure in the same program with the same name. If you want to use an API procedure both ways, you can declare it with an Alias name:
    Declare Sub SetTextAlign Lib “GDI” (...)
    Declare Function SetTextAlignFunction Lib “GDI” Alias _
        “SetTextAlign” (...) As Long
You can then call it using whichever declaration fits the situation at hand:
    Call SetTextAlign (...)
or
    RetVl = SetTextAlignFunction (...)
#### A Windows API Demonstration The program I will develop in this section not only shows you a real example of using an API procedure, but it also does something useful. When you use Visual Basic’s Print method to display text on a form or a Picture Box, the position of the text is determined by the object’s CurrentX and CurrentY properties, which give the horizontal and vertical positions of the current position where the text will appear. The text is always left-aligned with respect to this position. Visual Basic itself provides no other alignment options. You can, however, use a Windows API call to obtain different types of text alignment—both vertical and horizontal. This is accomplished with the SetTextAlign procedure. Its declaration is:
    Declare Sub SetTextAlign Lib “gdi32” (ByVal hDC As Long, ByVal wFlags _
       As Long)
The argument hDC specifies the hDC property of the object you are printing to, and the argument wFlags contains the flags specifying the desired text alignment. These flags specify the text justification with respect to the object’s current position. You can specify both vertical and horizontal justification. The possible values for horizontal alignment are defined by the following Windows global constants: • TA_CENTER—Centers the text at the current position • TA_LEFT—Positions the left edge of the text at the current position (the default) • TA_RIGHT—Positions the right edge of the text at the current position You can also specify vertical alignment. To understand vertical alignment, consider the total vertical extent that characters can span, using these terms: • BOTTOM—The lowest any character extends, such as the descenders on g and y • TOP—The highest any character extends, such as tall uppercase letters or diacritical marks, such as Ä and é • BASELINE—The bottom of characters that don’t have descenders, such as a and c The vertical alignment options are defined using the following terms: • TA_BASELINE—Font baseline aligned with the current position • TA_BOTTOM—Font bottom aligned with the current position • TA_TOP—Font top aligned with the current position (the default) Because the available text alignment settings operate with respect to the object’s current position, the SetTextAlign procedure does not align text with respect to the edges of the print area, as a word processing program does. If you want to specify alignment with respect to the object edges, you must do some fiddling in code. For example, to right-align text in a Picture Box so that the right edge of the text lines up with the right edge of the Picture Box, set the Picture Box’s CurrentX property to the right edge (obtained from the ScaleWidth property), then use the SetTextAlign procedure to set right alignment before displaying the text with the Print method. Similarly, to center text in a Picture Box, first set CurrentX to the center of the box (ScaleWidth/2) and then use SetTextAlign to specify center alignment.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.